package com.hmyd.hmyd_android.ui;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Bank;
import com.hmyd.hmyd_android.dto.Deposit;
import com.hmyd.hmyd_android.dto.SinaDetail;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 充值
 */
@ContentView(R.layout.activity_recharge)
public class RechargeActivity extends BasicActivity {
	private final int BANKCARD = 1;
	private final int DODEPOSIT = 2;
	private final int BINDINGADVANCE = 3;

	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	// 银行Logo
//	@ViewInject(R.id.bank_logo)
//	private ImageView bank_logo;

	// 充值
	@ViewInject(R.id.withdrawals)
	private EditText withdrawals;

	// 充值说明
	@ViewInject(R.id.assets_cash_fee_description)
	private TextView assets_cash_fee_description;
	// 充值提交
	@ViewInject(R.id.submit)
	private Button submit;
	
	private Bank bank;
	private String available_balance;

	private static final String BANK = "bank";
	private static final String AVAILABLE_BALANCE = "available_balance";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	private void onRequest() {
		onRequest(BANKCARD, UrlConstant.DEPOSIT, HttpMethod.GET, null);
	}

	String bindingPayCardId; // 支付银行卡ID
	String tradeNo; // 支付订单编号

	@Override
	public void onPostSuccess(int postId, String msg) {
		Gson gson = new Gson();

		switch (postId) {
		case BANKCARD: // 银行卡信息
			java.lang.reflect.Type type = new TypeToken<Bank>() {
			}.getType();
			Bank bank = gson.fromJson(msg, type);
			
			setData(bank);
			break;
		case DODEPOSIT: // 充值
			java.lang.reflect.Type type1 = new TypeToken<SinaDetail>() {
			}.getType();
			SinaDetail sinaDetail = gson.fromJson(msg, type1);
		    
		    if (null == sinaDetail) return;
		    
		    if (sinaDetail.code.equalsIgnoreCase("success")) {
				Utils.jumpURL(this, sinaDetail.message, "新浪平台管理充值", "", false);
				AppBasic.getInstance().finishActivity(this);
			}
			
			break;
		case BINDINGADVANCE:
			java.lang.reflect.Type bindType = new TypeToken<Deposit>() {
			}.getType();

			Deposit deposit2 = gson.fromJson(msg, bindType);

			MyToast.showToast(this, deposit2.message);
			AppBasic.getInstance().finishCurrentActivity();

			break;
		default:
			break;
		}
	}
	
	@Override
	public void onPostFailure(int postId, String msg) {
		switch (postId) {
		case BANKCARD:
			AppBasic.getInstance().finishCurrentActivity();
			break;
		case BINDINGADVANCE:
			MyToast.showToast(this, "充值失败，详情请联系客服！");

			submit.setText(getString(R.string.recharge));
			break;
		default:
			break;
		}
	}
	
	private void setData(Bank bank){

		bindingPayCardId = bank.cardId;
	}

	/**
	 * 充值
	 */
	private void getCode(String amount) {

		RequestParams params = new RequestParams();
		params.addBodyParameter("amount", amount);
		onRequest(DODEPOSIT, UrlConstant.DODEPOSIT, HttpMethod.POST, params);
	}

	// 充值说明
	@OnClick(R.id.assets_cash_fee_description)
	private void descriptionClick(View v) {
		if (AppBasic.app != null
				&& !TextUtils.isEmpty(AppBasic.app.rechargeInstructionsLinkUrl)) {
			Utils.jumpURL(this, AppBasic.app.rechargeInstructionsLinkUrl,
					getString(R.string.assets_recharge_description), "", false);
		}
	}

	// 充值提交
	@OnClick(R.id.submit)
	private void submitClick(View v) {
		String validCode = withdrawals.getText().toString().trim();
		if (TextUtils.isEmpty(validCode)) { // 充值金额判空
			withdrawals.setFocusable(true);
			MyToast.showHMToast(this, false,
					getString(R.string.assets_recharge_hint));
			return;
		}else {
//				submit.setText("充值中...");
				String amount = withdrawals.getText().toString().trim();
				getCode(amount);
				
//				if (AppBasic.app != null
//						&& !TextUtils.isEmpty(AppBasic.app.message)) {
//					Utils.jumpURL(this, AppBasic.app.message,
//							getString(R.string.sina_recharge), "", false);
//				}
			}
//			setSubmitBg(true);
	}

//	/**
//	 * 设置充值提交按钮背景
//	 */
//	@SuppressWarnings("deprecation")
//	private void setSubmitBg(boolean isPressed) {
//		if (isPressed) {
//			submit.setEnabled(false);
//			submit.setBackgroundColor(getResources().getColor(
//					R.color.code_pressed_bg));
//		} else {
//			submit.setEnabled(true);
//			submit.setBackgroundDrawable(getResources().getDrawable(
//					R.drawable.recharge_shape_pressed));
//		}
//	}


	@Override
	protected void setParams() {
		home_title_txt.setText(getString(R.string.recharge));
		home_right.setText(getString(R.string.assets_recharge_detail));
		home_title.setOnTouchListener(touch);
		
		bank = (Bank) getIntent().getSerializableExtra(BANK);

		available_balance = getIntent().getStringExtra(AVAILABLE_BALANCE);
		if (TextUtils.isEmpty(available_balance)) {
			withdrawals.setHint("请输入充值金额!");
		} else {
			withdrawals.setHint("可用余额：" + available_balance + "元");
		}
		
		if (null == bank) {
			onRequest();
		} else {
			setData(bank);
		}
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {
		startActivity(new Intent(this, RechargeHistoryListActivity.class));
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

//		handler.removeCallbacksAndMessages(null);
	}

}
